/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 
grApp.controller('IndexCtrl', ['$scope',
    '$http','IndexFactory',
    function ($scope, $http,indexFactory) {
        $scope.fieldList=null;
        $scope.fieldNameArray=new Array();
        $scope.fieldNameArrayStr=null;
        $scope.requestStr=null;
        $scope.tablename=null;
        $scope.cURDAddCode=null;
        $scope.cURDEditCode=null;
        $scope.cURDListCode=null;
        $scope.checkeboxFlag=false;
        //字段全选选项按钮切换
        $scope.CheckedAll=function(){
            for(var i=0;i<$scope.fieldList.length;i++){
               $scope.fieldList[i]['checked']=$scope.checkeboxFlag;
            };
            $scope.Checked();
            
        };
        
        $scope.Checked=function(){
            $scope.afterCreat(); //进行先加工 组织待加工字段 
            indexFactory.getQueryFieldList($scope.fieldNameArrayStr).then(function(data){
                $scope.requestStr=data.RequestStr;
            });
        };
        
        //表选择执行
        $scope.chooseTable=function(tablename){
            $scope.tablename=tablename;
             $scope.checkeboxFlag=false;
            indexFactory.getFieldList(tablename).then(function(data){
                $scope.fieldList=data.fields;
            });
        };
        //代码生成按钮
        $scope.codeCreate=function(){
            
            //生成CURD ADD
            indexFactory.getCURDAddCode($scope.tablename,$scope.fieldNameArrayStr).then(function(data){
                $scope.cURDAddCode=data.codes;
                
            });
            //生成CURD EDIT
            indexFactory.getCURDEditCode($scope.tablename,$scope.fieldNameArrayStr).then(function(data){
                $scope.cURDEditCode=data.codes;
            });
             //生成CURD Del
            indexFactory.getCURDDelCode($scope.tablename,$scope.fieldNameArrayStr).then(function(data){
                $scope.cURDDelCode=data.codes;
            });
            //生成 getChangeStatusCode
              indexFactory.getChangeStatusCode($scope.tablename,$scope.fieldNameArrayStr).then(function(data){
                $scope.ChangeStatusCode=data.codes;
            });
            //生成CURD LIST
            indexFactory.getCURDListCode($scope.tablename,$scope.fieldNameArrayStr).then(function(data){
                $scope.cURDListCode=data.codes;
            });
        };
        
        //字段文字
        $scope.afterCreat=function(){
           for(var i=0;i<$scope.fieldList.length;i++){
               if($scope.fieldList[i]['checked']==true){
                    $scope.fieldNameArray.push($scope.fieldList[i]['name']);
               }
           }  
           
           $scope.fieldNameArrayStr="";
           $scope.fieldNameArrayStr = $scope.fieldNameArray.join(",");
           $scope.fieldNameArray=new Array();
        };
        
    }
    
]);


grApp.factory('IndexFactory',['$http','$q',function($http,$q){
    var service={};
    //活动表的字段列表
    service.getFieldList=function(tablename){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:'/tools/index.php/Home/Ajax/getFieldList',
            params:{tablename:tablename}
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    };
    //读取待处理字段
    service.getQueryFieldList=function(fieldListstr){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:'/tools/index.php/Home/Ajax/getQueryFieldList',
            params:{fieldListstr:fieldListstr}
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    };
    //读取curd
    service.getCURDListCode=function(tablename,fieldListstr){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:'/tools/index.php/Home/Ajax/getCURDListCode',
            params:{tablename:tablename,fieldListstr:fieldListstr}
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.getCURDAddCode=function(tablename,fieldListstr){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:'/tools/index.php/Home/Ajax/getCURDAddCode',
            params:{tablename:tablename,fieldListstr:fieldListstr}
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.getCURDEditCode=function(tablename,fieldListstr){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:'/tools/index.php/Home/Ajax/getCURDEditCode',
            params:{tablename:tablename,fieldListstr:fieldListstr}
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.getCURDDelCode=function(tablename,fieldListstr){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:'/tools/index.php/Home/Ajax/getCURDDelCode',
            params:{tablename:tablename,fieldListstr:fieldListstr}
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.getChangeStatusCode=function(tablename,fieldListstr){
        var deferred=$q.defer();
        $http({
            method:'POST',
            url:'/tools/index.php/Home/Ajax/getChangeStatusCode',
            params:{tablename:tablename,fieldListstr:fieldListstr}
        }).success(function(data,status,headers,config){
            deferred.resolve(data);
        }).error(function(data,status,headers,config){
            deferred.reject(data);
        });
        return deferred.promise;
    };
    
    //返回
    return service;
}]);